﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JJY.CommonHelper;
using HtmlAgilityPack;
using League.Entity;
using System.Text;
using League.Business;
using League.DataAccess;

public partial class Page_DataCollectionByList : System.Web.UI.Page
{
    private const string mainUrl = "http://goal.sports.163.com";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtIssue.Text = "http://goal.sports.163.com/21/schedule/team/0_{0}_{1}.html";
        }
    }


    protected void btnSeason_Click(object sender, EventArgs e)
    {
        GetData(txtSeason.Text);
    }

    protected void btnIssue_Click(object sender, EventArgs e)
    {
        string url = txtIssue.Text;

        int intFrom = 0, intTo = 0;
        if (int.TryParse(txtFrom.Text, out intFrom) && int.TryParse(txtTo.Text, out intTo))
        {
            if (intFrom < intTo)
            {
                for (int i = intFrom; i <= intTo; i++)
                {
                    try
                    {
                        string urlTemp = string.Format(url, i, ddlSeason.SelectedValue);
                        GetData(urlTemp);
                    }
                    catch
                    { }
                }
            }
        }
    }

    private void GetData(string url)
    {
        string strMatchList = HttpRequestService.GetHtmlSourceByUrl(url, Encoding.UTF8)
                .HtmlToString().Trim().ToUTF8();

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(strMatchList);

        string leagueName = doc.DocumentNode.SelectNodes("//div[@class='c2']")[0].InnerText.Trim();
        HtmlNodeCollection nodeTrSet = doc.DocumentNode.SelectNodes("//table[@class='daTb01']/tr");
        
        if (nodeTrSet != null && nodeTrSet.Count > 0)
        {
            //remove title
            for (int i = 1; i < nodeTrSet.Count; i++)
            {
                HtmlNode trMatch = nodeTrSet[i];
                HtmlNodeCollection tdCollection = trMatch.SelectNodes("td");
                if (tdCollection != null && tdCollection.Count == 8)
                {
                    DateTime matchDateTime = new DateTime();

                    if (DateTime.TryParse(tdCollection[1].InnerText, out matchDateTime))
                    {
                        HtmlNode trStats = nodeTrSet[i + 1];
                        HtmlNodeCollection aCollectionSet = trStats.SelectNodes("td")[0].SelectNodes("a");
                        string statsUrl = mainUrl + aCollectionSet.Where(a => a.InnerText.Trim() == "统计")
                                                                  .FirstOrDefault().Attributes["href"].Value;

                        string jsonUrl = statsUrl.Replace("stat", "team").Replace("html", "json");
                        string strJson = HttpRequestService.GetHtmlSourceByUrl(jsonUrl, Encoding.UTF8);

                        if (strJson.IndexOf("error: (404)") < 0)
                        {
                            TeamMatchDataOrganizeMonitor m = new TeamMatchDataOrganizeMonitor(statsUrl, jsonUrl, strJson);

                            //TeamAllDataController.Insert(matchDateTime, m._LeagueMatch, m._LeagueInfo, m._HomeTeam, 
                            //    m._VisitingTeam, m._LeagueMatchStat);
                            TeamAllDataController.Insert(matchDateTime, m._LeagueMatch, m._LeagueInfo, m._HomeTeamData,
                                m._VisitingTeamData, m._LeagueMatchStat);
                        }
                        else
                        {
                            byte roundNumber = byte.Parse(nodeTrSet[i].ChildNodes[1].InnerText.Trim());
                            string homeTeamName = nodeTrSet[i].ChildNodes[7].InnerText.Trim();
                            string visitingTeamName = nodeTrSet[i].ChildNodes[11].InnerText.Trim();
                            

                            TeamAllDataController.InsertFutureMatch(leagueName, short.Parse(ddlSeason.SelectedValue), 
                                matchDateTime, roundNumber, homeTeamName, visitingTeamName);
                        }
                    }
                }
            }
        }
    }
}